package main

import (
	"fmt"
	"github.com/guonaihong/gout"
	"gowork/reptiles/pool"
	"strconv"
	"time"
)

func TestSimplePool() {
	p := pool.NewSimplePoll(20)
	for i := 0; i < 100; i++ {
		p.Add(parseTask(i))
	}
	p.Run()
}

type Subjects struct {
	Subjects []Subject `json:"subjects"`
}

type Subject struct {
	Cover string `json:"cover"`
	Cover_x int `json:"cover_x"`
	Cover_y int `json:"cover_y"`
	Id string `json:"id"`
	Is_new string `json:"is_new"`
	Playable string `json:"playable"`
	Rate string `json:"rate"`
	Title string `json:"title"`
	Url string `json:"url"`
}

type CheckAttendanceSheet struct {
	Id int `json:"id"`
	AttendanceSheetId int `json:"attendanceSheetId"`
	AttendanceSheetState int `json:"attendanceSheetState"`
	Pass int `json:"pass"`
	CreatedAt time.Time `json:"createdAt"`
	UpdatedAt time.Time `json:"updatedAt"`
}

func parseTask(i int) func() {
	return func() {
		// 模拟抓取数据的过程
		time.Sleep(time.Second * 1)
		var subjects Subjects
		//var checkAttendanceSheets []CheckAttendanceSheet
		//gout.POST("localhost:0007/findCheckAttendanceSheet").SetForm(gout.H{
		//	"staffId": "3",
		//},).BindJSON(&checkAttendanceSheets).Do()
		//fmt.Println("finish parse ", checkAttendanceSheets)

		gout.GET("https://movie.douban.com/j/search_subjects?type=movie&tag=热门&sort=recommend&page_limit=20&page_start="+strconv.Itoa(i)).
			SetHeader(gout.H{"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36"}).
			BindJSON(&subjects).Do()
		fmt.Println("finish parse： ", subjects, len(subjects.Subjects))

		fmt.Println("finish parse i：", i)

		//resp, err := http.Get("https://movie.douban.com/j/search_subjects?type=movie&tag=热门&sort=recommend&page_limit=20&page_start="+strconv.Itoa(i))
		//if err != nil {
		//	return
		//}
		//defer resp.Body.Close()
		//body, _ := ioutil.ReadAll(resp.Body)
		//fmt.Println(string(body))
		//_ = json.Unmarshal(body,&subjects)
		//fmt.Printf("%#v", subjects)
	}
}

func main()  {
	TestSimplePool()
	//var subjects Subjects
	//gout.GET("https://movie.douban.com/j/search_subjects?type=movie&tag=热门&sort=recommend&page_limit=20&page_start=1").BindJSON(&subjects).Do()
	//fmt.Println("finish parse ", subjects)

	//var checkAttendanceSheets []*CheckAttendanceSheet
	//gout.POST("localhost:0007/findCheckAttendanceSheet").SetForm(gout.H{
	//	"staffId": 3,
	//},).BindJSON(&checkAttendanceSheets).Do()
	//fmt.Println("finish parse ", checkAttendanceSheets)
	//
	//swag.Println("swagger")
}
